package com.reggielcy.filter;

import com.alibaba.fastjson.JSON;
import com.reggielcy.common.BaseContext;
import com.reggielcy.common.R;
import jdk.nashorn.internal.ir.debug.JSONWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.compiler.ast.Variable;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther teacher
 * @date 2024/3/27
 * @project reggie_take_out
 */
@Slf4j
@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
    private static final AntPathMatcher ANT_PATH_MATCHER=new AntPathMatcher();
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpServletResponse response=(HttpServletResponse) servletResponse;

        log.info("拦截的请求是: {}",request.getRequestURL());

        //1.获取本次请求的URI
        String uri=request.getRequestURI();
        //定义不需要处理的请求路径
       /* List<String> urls=new ArrayList<>();
        urls.add("/employee/login");
        urls.add("/employee/logout");
        urls.add("/backend/**");
        urls.add("/front/**");*/
        String[] urls = new String[]{
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/user/login",
                "/user/sendSms",


        };


        //2.判断本次请求是否需要处理
       Boolean check=check(urls,uri);
        //3.如果不需要处理直接放行
        if (check){
            log.info("拦截的请求不需要处理: {}",request.getRequestURL());
            filterChain.doFilter(request,response);
            return;
        }

        //4.1后台如果已登录则直接放行
        if(request.getSession().getAttribute("employee")!=null){
            Long empId=(Long) request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(empId);
            filterChain.doFilter(request,response);
            return;
        }
        //4.2移动端如果已登录则直接放行
        if(request.getSession().getAttribute("user")!=null){
            Long userId=(Long) request.getSession().getAttribute("user");
            BaseContext.setCurrentId(userId);
            filterChain.doFilter(request,response);
            return;
        }



        // 输出流的方式告诉前端 未登录

        // 返给前端 未登录的消息
        log.info("未登录");
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
    }

    private Boolean check(String [] urls, String uri) {
        for (String url : urls) {
            boolean match = ANT_PATH_MATCHER.match(url,uri);
            if (match) {
                return true;
            }
        }
        return false;
    }
}
